import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useStudentStore = defineStore('student', () => {
  // 学生数据
  const students = ref([
    {
      id: 1,
      date: '2016-05-03',
      name: '张三',
      address: '江苏省苏州市',
    },
    {
      id: 2,
      date: '2016-05-02',
      name: '李四',
      address: '山西省太原市',
    },
    {
      id: 3,
      date: '2016-05-04',
      name: '王五',
      address: '山西省临汾市',
    },
    {
      id: 4,
      date: '2016-05-01',
      name: '赵六',
      address: '山西省晋城市',
    },
  ])

  // 日志数据
  const logs = ref([
    {
      id: 1,
      date: '2025/9/11 10:30:22',
      user: '管理员',
      action: '学生信息查询',
      detail: '查询了学生张三的信息',
    },
    {
      id: 2,
      date: '2025/9/11 09:15:47',
      user: '张老师',
      action: '学生信息录入',
      detail: '录入了学生李四的信息',
    },
    {
      id: 3,
      date: '2025/9/11 16:22:18',
      user: '管理员',
      action: '系统登录',
      detail: '登录学生管理系统',
    },
    {
      id: 4,
      date: '2025/9/12 08:45:33',
      user: '王老师',
      action: '学生信息修改',
      detail: '修改了学生王五的地址信息',
    },
  ])

  // 计算属性：学生总数
  const totalStudents = computed(() => students.value.length)

  // 添加学生
  function addStudent(student: { date: string; name: string; address: string }) {
    const newStudent = {
      id: students.value.length > 0 ? Math.max(...students.value.map(s => s.id)) + 1 : 1,
      ...student
    }
    students.value.push(newStudent)
    
    // 添加日志
    addLog({
      date: new Date().toLocaleString(),
      user: '管理员',
      action: '学生信息录入',
      detail: `录入了学生${student.name}的信息`
    })
  }

  // 编辑学生
  function editStudent(id: number, updatedStudent: { date: string; name: string; address: string }) {
    const index = students.value.findIndex(student => student.id === id)
    if (index !== -1) {
      const oldName = students.value[index].name
      students.value[index] = { ...students.value[index], ...updatedStudent }
      
      // 添加日志
      addLog({
        date: new Date().toLocaleString(),
        user: '管理员',
        action: '学生信息修改',
        detail: `修改了学生${oldName}的信息`
      })
    }
  }

  // 删除学生
  function deleteStudent(id: number) {
    const index = students.value.findIndex(student => student.id === id)
    if (index !== -1) {
      const deletedStudent = students.value[index]
      students.value.splice(index, 1)
      
      // 添加日志
      addLog({
        date: new Date().toLocaleString(),
        user: '管理员',
        action: '删除学生信息',
        detail: `删除了学生${deletedStudent.name}的信息`
      })
    }
  }

  // 添加日志
  function addLog(log: { date: string; user: string; action: string; detail: string }) {
    const newLog = {
      id: logs.value.length > 0 ? Math.max(...logs.value.map(l => l.id)) + 1 : 1,
      ...log
    }
    logs.value.push(newLog)
  }

  // 搜索学生
  function searchStudents(name: string, address: string) {
    return students.value.filter(student => {
      const nameMatch = name ? student.name.includes(name) : true
      const addressMatch = address ? student.address.includes(address) : true
      return nameMatch && addressMatch
    })
  }

  return {
    students,
    logs,
    totalStudents,
    addStudent,
    editStudent,
    deleteStudent,
    addLog,
    searchStudents
  }
})